import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_taotaoshipin/base/base_refresh_controller.dart';
import 'package:flutter_taotaoshipin/http/dao/user_dao.dart';
import 'package:flutter_taotaoshipin/http/dao/userinfo_dao.dart';
import 'package:flutter_taotaoshipin/model/user_drama_record_entity.dart';
import 'package:get/get.dart';

import 'me_state.dart';

class MeController extends BaseRefreshController {
  final MeState state = MeState();

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  @override
  fetchData({bool showLoading = true}) async {
    // 开始加载
    if (showLoading) {
      loadStatus = 0;
      update();
    }

    try {
      await UserinfoDao.updateCenter();

      // 加载历史
      state.historyList = await _loadHistoryData();

      if (showLoading) {
        loadStatus = 1;
      }
    } catch (e) {
      if (showLoading) {
        loadStatus = 3;
      } else {
        EasyLoading.showError('加载失败');
      }
    }
    update();
    // 结束下拉
    refreshController.refreshCompleted(resetFooterState: true);
  }

  /// 获取历史
  Future<List<UserDramaRecordEntity>> _loadHistoryData() async {
    List<UserDramaRecordEntity> list = [];
    try {
      var result = await UserDao.dramaRecord();
      if (result.code == 0) {
        if (result.data != null) {
          list = result.data!;
        }
      }
    } catch (_) {}
    return list;
  }
}
